using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace NewFeatures4 {//Define interface and classes public interface MyInterface //Interface with out modifier { T GetItemAt(int index); } public class MyClass : MyInterface //Implement interface { public MyClass(T t1, T t2, T t3) { _data.Add(t1); _data.Add(t2); _data.Add(t3); } public T GetItemAt(int index)//Return item at index {//Return item of List at index return _data[index]; } //Create List of Ts public List _data = new List(); } public class FooClass { public static T GetThirdItem(MyInterface foo)//Return empty list of Ts { return foo.GetItemAt(2); } } //The new out keyword means IEnumerable is now read only in .Net 4.0 //public interface IEnumerable : IEnumerable //covariant // { // IEnumerator GetEnumerator(); // } class Variance { static void Main(string[] args) { //Back to basics String s = "hello"; Object o = new Object(); o = s; //s = o; //Will not complile // Is a -list- of strings a -list- of objects? List strings = new List(); List objects = new List(); strings.Add("hello"); //objects = strings; //Will not complile //objects.Add(123); objects.AddRange(strings); objects.Add(123); //Add elements of objects to strings //This works in 4.0 as IEnumerable defined with out modifier MyInterface myobjects = new MyClass(1,2,3); MyInterface mystrings = new MyClass("a", "b", "c"); o = FooClass.GetThirdItem(myobjects); o = FooClass.GetThirdItem(mystrings); //This works in 4.0 as MyInterface uses out modifier Console.ReadLine(); } } }